﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Google.GData.Client;
using Google.GData.Spreadsheets;

namespace GoogleFeeder
{
    public class Spreadsheets
    {
        public readonly SpreadsheetsService Service;

        public Spreadsheets(string username, string password)
        {
            Service = new SpreadsheetsService("MySpreadsheetIntegration-v1");
            Service.Credentials = new GDataCredentials(username, password);
        }

        public List<SpreadsheetEntry> Sheets
        {
            get
            {
                SpreadsheetQuery query = new SpreadsheetQuery();
                SpreadsheetFeed feed = Service.Query(query);
                return feed.Entries.ToList().Cast<SpreadsheetEntry>().ToList();
            }
        }

        public Spreadsheet GetSheetByName(string name)
        {
            var sheets = Sheets;
            foreach (SpreadsheetEntry tmp in sheets)
            {
                string title = tmp.Title.Text;
                if (title == name)
                    return new Spreadsheet(this, tmp);
            }
            throw new Exception("Sheet named \"" + name + "\" not found");
        }
    }
}
